package edu.neu.chenhan.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import edu.neu.chenhan.R;
import edu.neu.chenhan.listener.OnItemClickListener;
import edu.neu.chenhan.listener.OnJoinMeetingListener;
import edu.neu.chenhan.model.Meeting;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class MeetingListAdapter extends RecyclerView.Adapter<MeetingListAdapter.MeetingViewHolder> {
    private Context context;
    private List<Meeting> meetingList;
    private OnItemClickListener itemClickListener;
    private OnJoinMeetingListener joinMeetingListener; // 参会按钮点击监听器

    public MeetingListAdapter(Context context, List<Meeting> meetingList,
                              OnItemClickListener itemClickListener,
                              OnJoinMeetingListener joinMeetingListener) {
        this.context = context;
        this.meetingList = meetingList;
        this.itemClickListener = itemClickListener;
        this.joinMeetingListener = joinMeetingListener;
    }

    @NonNull
    @Override
    public MeetingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_meeting, parent, false);
        return new MeetingViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MeetingViewHolder holder, int position) {
        Meeting meeting = meetingList.get(position);
        holder.tvMeetingName.setText(meeting.getName());
        holder.tvMeetingDescription.setText(meeting.getDescription());
        holder.tvMeetingTime.setText(formatMeetingDate(meeting.getStartTime()));

        // 绑定参会按钮并设置位置标签
        holder.btn_attend_meeting.setTag(position);

        // 为参会按钮设置点击事件
        holder.btn_attend_meeting.setOnClickListener(v -> {
            int pos = (int) v.getTag();
            if (joinMeetingListener != null) {
                joinMeetingListener.onJoinClick(pos);
            }
        });
    }

    @Override
    public int getItemCount() {
        return meetingList != null ? meetingList.size() : 0;
    }

    public Meeting getItem(int position) {
        return meetingList.get(position);
    }

    // 日期格式化工具方法
    private String formatMeetingDate(Object dateObj) {
        if (dateObj == null) return "未设置时间";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        try {
            if (dateObj instanceof Date) {
                return sdf.format((Date) dateObj);
            } else if (dateObj instanceof Long) {
                return sdf.format(new Date((Long) dateObj));
            } else if (dateObj instanceof String) {
                // 尝试解析字符串格式的日期
                try {
                    return sdf.format(sdf.parse((String) dateObj));
                } catch (Exception e) {
                    Log.e("MeetingAdapter", "日期解析失败: " + dateObj, e);
                    return (String) dateObj;
                }
            }
        } catch (Exception e) {
            Log.e("MeetingAdapter", "日期格式化失败", e);
        }

        return "日期格式错误";
    }

    public static class MeetingViewHolder extends RecyclerView.ViewHolder {
        TextView tvMeetingName, tvMeetingDescription, tvMeetingTime;
        Button btn_attend_meeting;

        public MeetingViewHolder(@NonNull View itemView) {
            super(itemView);
            tvMeetingName = itemView.findViewById(R.id.tv_meeting_name);
            tvMeetingDescription = itemView.findViewById(R.id.tv_meeting_description);
            tvMeetingTime = itemView.findViewById(R.id.tv_meeting_time);
            btn_attend_meeting = itemView.findViewById(R.id.btn_attend_meeting); // 绑定参会按钮
        }
    }
}